package com.kecheng.dao;

import com.kecheng.hibernate.BaseDao;
import com.kecheng.moban.PageBean;
import com.kecheng.pojo.Team;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
@Component
public class TeamDao extends BaseDao {
	@Value("${hibernate.pageSize}")
	private int pageSize;
	@Value("${hibernate.page}")
	private int page;

	/**
	 * 根据姓名，微信昵称，电话号码模糊查询
	 * @param searchVal
	 * @return
	 */
	public PageBean<Team> search(String searchVal){
		PageBean<Team> pageBean = new PageBean<>();
		pageBean.setPage(page);
		pageBean.setPageSize(pageSize);

		DetachedCriteria criteria=DetachedCriteria.forClass(Team.class);
		criteria.add(Restrictions.or(Restrictions.ilike("name", searchVal, MatchMode.ANYWHERE)
				,Restrictions.ilike("nickname", searchVal, MatchMode.ANYWHERE)
				,Restrictions.ilike("phone", searchVal, MatchMode.ANYWHERE)));
		criteria.addOrder(Order.desc("id"));
		List<Team> teams= (List<Team>) this.getHibernateTemplate().findByCriteria(criteria, (page-1)*pageSize, pageSize);

		List<Team> teamsAll = (List<Team>) this.getHibernateTemplate().findByCriteria(criteria);
		pageBean.setData(teams);
		pageBean.setTotal(teamsAll==null?0:teamsAll.size());
		pageBean.setTotalPage(computedAllPage(teamsAll==null?0:teamsAll.size()));

		return pageBean;
	}

	/**
	 * 根据数量计算页数
	 * @param count
	 * @return
	 */
	private Integer computedAllPage(Integer count) {
		int countint = count;
		if (countint % pageSize == 0) {
			if (countint < pageSize) {
				return 1;
			} else {
				return countint / pageSize;
			}
		} else {
			return countint / pageSize + 1;
		}
	}











	//****************************************以下是原始项目的方法*********************************************

	public void add(Team team) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(team);
	}

	public void delete(Team team) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().delete(team);
	}

	public void update(Team team) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(team);
	}

	public Team findbysellerid(String sellerid) {
		// TODO Auto-generated method stub
		List<Team> teams= (List<Team>) this.getHibernateTemplate().find("from Team where openid=? ",sellerid);
		
		if(teams.size()>0){
			Team team=teams.get(0);
			return team;
		}
		return null;
	}
	
	public Team findbysid(int sid) {
		// TODO Auto-generated method stub
		List<Team> teams= (List<Team>) this.getHibernateTemplate().find("from Team where id=?",sid);
		
		for(int i=0;i<teams.size();i++){
			Team team=teams.get(i);
			return team;
		}
		return null;
	}

	public List<Team> findall(int page) {
		
		int rowsPerPage =20;

		DetachedCriteria criteria=DetachedCriteria.forClass(Team.class);
		
		criteria.addOrder(Order.desc("id"));

		List<Team> teams= (List<Team>) this.getHibernateTemplate().findByCriteria(criteria, (page-1)*rowsPerPage, rowsPerPage);
		
		
		return teams;
	}
	
	
	public int findallpage() {
		
		// TODO Auto-generated method stub
		String hql = "select count(*) from Team ";

		Long count = (Long)getHibernateTemplate().find(hql).listIterator().next();
        System.out.println(count);
        int count1=count.intValue();
        int pageSize=20;
        if(count1 % pageSize == 0) {
			return count1/pageSize;
		} else {
			return count1/pageSize+1;
		}
	
       
}

	public List<Team> findbyrefereeid(int refereeid) {
		// TODO Auto-generated method stub
		List<Team> teams= (List<Team>) this.getHibernateTemplate().find("from Team where  refereeid =? order by id desc",refereeid);
		return teams;
	}

	public Team findoid(String oid) {
		// TODO Auto-generated method stub
		List<Team> teams= (List<Team>) this.getHibernateTemplate().find("from Team where oid=?",oid);

		if(teams.size()>0)
		{    
           Team  team=teams.get(0);       
		return team;      
		}
		return null;
		
	}

	public int findnum() {
		// TODO Auto-generated method stub
		String hql = "select count(*) from Team as team";

		Long count = (Long) getHibernateTemplate().find(hql).listIterator().next();
		int a=new Long(count).intValue();
		return a;
	}

	public List<Team> order(int page) {
		// TODO Auto-generated method stub
		
		int rowsPerPage =20;

		DetachedCriteria criteria=DetachedCriteria.forClass(Team.class);
		
		criteria.addOrder(Order.desc("allmoney"));

		List<Team> teams= (List<Team>) this.getHibernateTemplate().findByCriteria(criteria, (page-1)*rowsPerPage, rowsPerPage);
		
		
		return teams;
		
		
	
		
	}

	public Team findbyid(int id) {
		// TODO Auto-generated method stub
		List<?> teams=this.getHibernateTemplate().find("from Team where id=?",id);
		if(teams.size()>0){
			Team team= (Team) teams.get(0);
			return team;
		}
	    return null;
		
		
	}

	public List<Team> findbyfour(Integer id) {
		// TODO Auto-generated method stub
		List<Team> teams= (List<Team>) this.getHibernateTemplate().find("from Team where   four =? order by id desc",id);
		return teams;
	}

	public List<Team> findbytree(Integer id) {
		// TODO Auto-generated method stub
		List<Team> teams= (List<Team>) this.getHibernateTemplate().find("from Team where tree =? order by id desc",id);
		return teams;
	}

	public List<Team> findbytime(String time1, String time2) {
		
		
		
		List<Team> teams= (List<Team>) this.getHibernateTemplate().find("from Team where time between ? and ? ",new String[]{time1,time2});
		
		return teams;
		
	}

	public List<Team> findbytime1(String time) {
		// TODO Auto-generated method stub
		
   List<Team> teams= (List<Team>) this.getHibernateTemplate().find("from Team where time = ?",time);
		
		return teams;
		
	}

	public List<Team> findall() {
		// TODO Auto-generated method stub
     List<Team> teams= (List<Team>) this.getHibernateTemplate().find("from Team ");
		
		return teams;
	}

	public List<Team> search(int key) {
		// TODO Auto-generated method stub
		
		int rowsPerPage =100;
		int page=1;
		DetachedCriteria criteria=DetachedCriteria.forClass(Team.class);
		criteria.add(Restrictions.eq("id", key));		
		criteria.addOrder(Order.desc("id"));
		List<Team> teams= (List<Team>) this.getHibernateTemplate().findByCriteria(criteria, (page-1)*rowsPerPage, rowsPerPage);
				
		return teams;
				
	}

	/**
	 * 排行榜方法有问题，要进来看看
	 */
	public List<Team> findpaihang() {
		// TODO Auto-generated method stub
		 
		int rowsPerPage =100;
        int page=1;
		DetachedCriteria criteria=DetachedCriteria.forClass(Team.class);		
		criteria.addOrder(Order.desc("allmoney"));
		List<Team> teams1= (List<Team>) this.getHibernateTemplate().findByCriteria(criteria, (page-1)*rowsPerPage, rowsPerPage);
		Team team;
		List<Team> teams=new ArrayList<Team>();
//		for(int i=0;i<teams1.size();i++){
//			if(teams1.get(i).getXiaofei()>0){
//				team=teams1.get(i);
//				teams.add(team);
//	    	}		
//		}
		return teams;
	}
	public List<Team> findpaihangjf() {
		DetachedCriteria criteria=DetachedCriteria.forClass(Team.class);		
		criteria.addOrder(Order.desc("allscore"));
		criteria.add(Restrictions.between("allscore", 0, 1000000));
		List<Team> teams1= (List<Team>) this.getHibernateTemplate().findByCriteria(criteria);
		
		if(teams1.size()>100){
			return teams1.subList(0, 100);
		}
		return teams1;
	}
	

}
